package com.sanshi.project.assets.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.entity.PrintItemEntity;
import com.sanshi.project.assets.service.PrintItemService;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 打印配置项
 *
 * @author nikon
 * @date 2023-11-16 17:14:30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/printItem")
@Tag(description = "printItem", name = "打印配置项管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class PrintItemController {

    private final PrintItemService printItemService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param printItem 打印配置项
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('assets_printItem_view')")
    public R getPrintItemPage(@ParameterObject Page page, @ParameterObject PrintItemEntity printItem) {
        LambdaQueryWrapper<PrintItemEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(printItem.getType()), PrintItemEntity::getType, printItem.getType());
        wrapper.eq(StrUtil.isNotBlank(printItem.getName()), PrintItemEntity::getName, printItem.getName());
        wrapper.eq(StrUtil.isNotBlank(printItem.getItem()), PrintItemEntity::getItem, printItem.getItem());
        return R.ok(printItemService.page(page, wrapper));
    }


    /**
     * 通过id查询打印配置项
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('assets_printItem_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(printItemService.getById(id));
    }

    /**
     * 新增打印配置项
     *
     * @param printItem 打印配置项
     * @return R
     */
    @Operation(summary = "新增打印配置项", description = "新增打印配置项")
    @SysLog("新增打印配置项")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('assets_printItem_add')")
    public R save(@RequestBody PrintItemEntity printItem) {
        return R.ok(printItemService.save(printItem));
    }

    /**
     * 修改打印配置项
     *
     * @param printItem 打印配置项
     * @return R
     */
    @Operation(summary = "修改打印配置项", description = "修改打印配置项")
    @SysLog("修改打印配置项")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('assets_printItem_edit')")
    public R updateById(@RequestBody PrintItemEntity printItem) {
        return R.ok(printItemService.updateById(printItem));
    }

    /**
     * 通过id删除打印配置项
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除打印配置项", description = "通过id删除打印配置项")
    @SysLog("通过id删除打印配置项")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('assets_printItem_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(printItemService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param printItem 查询条件
     * @param ids       导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('assets_printItem_export')")
    public List<PrintItemEntity> export(PrintItemEntity printItem, Long[] ids) {
        return printItemService.list(Wrappers.lambdaQuery(printItem).in(ArrayUtil.isNotEmpty(ids), PrintItemEntity::getId, ids));
    }
}